package com.draw.cli.gui.panel.home;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.Loading;
import com.draw.cli.gui.img.IconManager;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.webclient.UserClient;
import com.draw.core.User;
import com.draw.webinterface.UserInterface;

public class HomePanel extends JPanel implements ActionListener, Refreshable , Runnable {
	private static final long serialVersionUID = -9148554811076965337L;
	
	private static final String NOTCONNECTED = "Vous n'êtes pas encore connecté(e).";

	private static HomePanel instance = new HomePanel();

	public static HomePanel getInstance() {
		return instance;
	}
	
	private JLabel title;
	private JLabel username;
	private JButton logout;

	public HomePanel() {
		this.setLayout(new BorderLayout());

		this.title = new JLabel("DrawSomeEnsi", IconManager.HOME_32, SwingConstants.CENTER);
		this.title.setFont(new Font("Arial", Font.BOLD, 20));
		this.add(this.title, BorderLayout.NORTH);

		JPanel pan = new JPanel(new GridLayout(1, 3));
		pan.add(SentDrawPanel.getInstance());
		pan.add(WaitingDrawPanel.getInstance());
		pan.add(AvailableDrawPanel.getInstance());
		this.add(pan, BorderLayout.CENTER);
		
		pan = new JPanel(new BorderLayout());
		pan.setBorder(BorderFactory.createTitledBorder("Utilisateur"));
		
		this.username = new JLabel(NOTCONNECTED);
		this.username.setHorizontalAlignment(JLabel.CENTER);
		pan.add(this.username);
		
		this.logout = new JButton("Deconnexion");
		this.logout.setEnabled(false);
		this.logout.addActionListener(this);
		pan.add(this.logout,BorderLayout.EAST);
		
		this.add(pan,BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent arg0) {
		if( this.logout.equals(arg0.getSource()) ) {
			ConfigManager.getInstance().setCurrentUser(null);
			this.refresh();
		}
	}

	@Override
	public void refresh() {
		new Thread(this).start();
	}

	@Override
	public void run() {
		Loading.start();
		SentDrawPanel.getInstance().refresh();
		WaitingDrawPanel.getInstance().refresh();
		AvailableDrawPanel.getInstance().refresh();
		User current = ConfigManager.getInstance().getCurrentUser();
		if ( current == null ) {
			this.username.setText(NOTCONNECTED);
			this.logout.setEnabled(false);
		} else {
			UserInterface ui;
			try {
				ui = UserClient.getInstance();
				current = ui.getUser(current.getUsername(), current.getPassword());
			} catch (MalformedURLException | WebServiceException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
			this.username.setText(current.getUsername()+" ("+current.getPoints()+" points)");
			this.logout.setEnabled(true);
		}
		Loading.stop();
	}
}
